package com.gzhryc.shared_device.oem.code.place.services;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.place.dao.PlacePhysicalStoreAddressDao;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStoreAddress;

import java.sql.SQLException;
import java.util.List;

public class PlacePhysicalStoreAddressService {

    static Logger log = Logger.getLogger(PlacePhysicalStoreAddressService.class);

    public static PlacePhysicalStoreAddressService self() {
        return MultiDBTools.getService(PlacePhysicalStoreAddressService.class, true);
    }

    public static PlacePhysicalStoreAddressService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, PlacePhysicalStoreAddressService.class, true);
    }

    PlacePhysicalStoreAddressDao dao;

    public PlacePhysicalStoreAddressService(String jdbcKey) {
        this.dao = new PlacePhysicalStoreAddressDao(jdbcKey);
    }

    public List<PlacePhysicalStoreAddress> findByIds(List<Long> placeIds){
        if(placeIds != null && placeIds.size() > 0){
            Conditions conditions = new Conditions(PlacePhysicalStoreAddress.class);
            conditions.and().in("placeId",placeIds);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public PlacePhysicalStoreAddress getById(Long id) {
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public boolean save(PlacePhysicalStoreAddress address) {
        PlacePhysicalStoreAddress temp = getById(address.getPlaceId());
        if (temp != null) {
            try {
                return dao.updateNotNull(address) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        } else {
            try {
                return dao.insert(address) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }
}
